/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 23, 2012  2:38:45 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.construction
 * File Name : PurchaseRequestItemServices.java
 * TODO
 */
package jpa.service.promis.construction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.construction.PurchaseRequestItem;
import jpa.entitys.promis.construction.PurchaseRequestItemPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class PurchaseRequestItemServices extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(PurchaseRequestItemServices.class);

	/**
	 * @param em
	 */
	public PurchaseRequestItemServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	

	public PurchaseRequestItem findById(PurchaseRequestItemPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "PurchaseRequestItem.findById"));				
	}
	

	public PurchaseRequestItem findBy6Key(PurchaseRequestItemPK id) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", id.getCompany());
		mapWhereValue.put("project", id.getProject());
		mapWhereValue.put("porType", id.getPorType());
		mapWhereValue.put("porSeries", id.getPorSeries());
		mapWhereValue.put("porNo", id.getPorNo());
		mapWhereValue.put("porSeqNo", id.getPorSeqNo());
		
		List<PurchaseRequestItem> lst = super.ConvertToYoureObject(super.find("PurchaseRequestItem.findBy6Key", mapWhereValue));
		if(CommonService.getSize(lst) == 1){
			return lst.get(0);
		}else
			throw new Exception("Error: Result more than one records [Size=" + CommonService.getSize(lst) + "]");
		
	}
	
	public PurchaseRequestItem InsertedOrUpdated(PurchaseRequestItem vlaue) throws Exception {
		PurchaseRequestItem obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{						
				super.inserted(obj);
			}else{				
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<PurchaseRequestItem> Inserted(List<PurchaseRequestItem> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(PurchaseRequestItem obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					common_lst.add(obj);
				}
			}		
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<PurchaseRequestItem> Updated(List<PurchaseRequestItem> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(PurchaseRequestItem obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{					
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
